package xin.pms.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import xin.common.utils.AssertUtils;
import xin.common.utils.StringUtils;
import xin.core.jpa.JpaConstant;
import xin.core.jpa.model.SimpleTreeUUIDEntity;
import xin.core.jpa.model.TreeUUIDEntity;
import xin.pms.enums.MenuMethod;
import xin.pms.enums.MenuType;
import xin.pms.enums.OptPermission;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 * Menu Model
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 15:34
 */
@Entity
@Table(name = "pms_menu")
@Data
@EqualsAndHashCode(callSuper = true, of = {})
@ToString(callSuper = true)
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Menu extends TreeUUIDEntity<Menu> implements Serializable{

    public static final String DEFAULT_ROOT_PARENT_ID = "root";

    @Column(length = JpaConstant.CommonStringLargeLength)
    private String lable;

    @Column(length = JpaConstant.CommonStringMidLength)
    private String bigIcon;

    @Column(length = JpaConstant.CommonStringLargeLength)
    private String image;

    @Enumerated(EnumType.STRING)
    @Column(length = JpaConstant.EnumStringLongColumnLength)
    private MenuMethod method = MenuMethod.blank;

    @Enumerated(EnumType.STRING)
    @Column(length = JpaConstant.EnumStringColumnLength)
    private MenuType type = MenuType.PAGE;


    @Column(length = JpaConstant.CommonStringMidLength)
    private String action;

    @Column(name="permission_code",length = JpaConstant.CommonStringLength)
    private String permissionCode;

    @Column
    private Boolean visible = true;

    @Column
    private Boolean deleted = false;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(updatable = false)
    protected Date createTime;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(insertable = false)
    protected Date lastModifyTime;

    public void  saveInit(MenuNode node, String parentId, int priority){
        this.action = node.getAction();
        this.name = node.getName();
        this.bigIcon = node.getBigIcon();
        this.description = node.getDescription();
        this.deleted = node.getDeleted() == null ? Boolean.FALSE : node.getDeleted();
        this.visible = node.getVisible() == null ? Boolean.TRUE : node.getVisible();
        this.icon = node.getIcon();
        this.image = node.getImage();
        this.lable = node.getLable();
        this.method = node.getMethod();
        this.type = node.getType();
        this.permissionCode = node.getPermissionCode();
        this.parentId = parentId;
        this.priority = priority;
        this.setLastModifyTime(new Date(System.currentTimeMillis()));
        this.setCreateTime(new Date(System.currentTimeMillis()));
    }

    public Menu updateInit(MenuNode node){
        if(StringUtils.isNotBlank(node.getId()))
            this.id = node.getId();
        if(StringUtils.isNotBlank(node.getAction()))
            this.action = node.getAction();
        if(StringUtils.isNotBlank(node.getName()))
            this.name = node.getName();
        if(StringUtils.isNotBlank(node.getBigIcon()))
            this.bigIcon = node.getBigIcon();
        if(StringUtils.isNotBlank(node.getDescription()))
            this.description = node.getDescription();
        this.deleted = node.getDeleted() == null ? this.getDeleted() : node.getDeleted();
        this.visible = node.getVisible() == null ? this.getVisible() : node.getVisible();
        if(StringUtils.isNotBlank(node.getIcon()))
            this.icon = node.getIcon();
        if(StringUtils.isNotBlank(node.getImage()))
            this.image = node.getImage();
        if(StringUtils.isNotBlank(node.getLable()))
            this.lable = node.getLable();
        if(node.getMethod() != null)
            this.method = node.getMethod();
        if(StringUtils.isNotBlank(node.getPermissionCode()))
            this.permissionCode = node.getPermissionCode();
        this.setLastModifyTime(new Date(System.currentTimeMillis()));
        return this;
    }
}
